package baduga.karo;

import javax.swing.JOptionPane;

import baduga.client.Game_Cfg;
import baduga.client.Network_Api;

import java.awt.Toolkit;
import javax.swing.JLabel;
import javax.swing.GroupLayout.Alignment;
import javax.swing.GroupLayout;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.JTextField;



//klasa z okienkiem do zakładania nowego stołu


/**
* @author Karolina
*/
public class CreateNewTable extends javax.swing.JFrame {

	private static final long serialVersionUID = 1L;
	boolean successTable = false;
  
	//zmiana ikonki na pasku tytułu
  public CreateNewTable() {
  	setIconImage(Toolkit.getDefaultToolkit().getImage(CreateNewTable.class.getResource("/baduga/karo/resources/icon.png")));
      initComponents();
  }
  int timeToMove = 0;
  int cash = 0;
  int bigBlind = 0;
  int smallBlind = 0;
  int numberPlayers = 0;
  int numberObs = 0;
  String tableName = "";
  
  @SuppressWarnings("unchecked")
                      
  private void initComponents() {
  	//dodajemy główne pola na ekranie
      jLabelCreateTabel = new javax.swing.JLabel();
      jButtonOK = new javax.swing.JButton();
      jButtonCancel = new javax.swing.JButton();
      jLabelRequirement = new javax.swing.JLabel();
      minimumCash = new javax.swing.JTextField();
      jLabel1 = new javax.swing.JLabel();
      jTextFieldBigBlindValue = new javax.swing.JTextField();
      jLabel2 = new javax.swing.JLabel();
      jTextFieldSmallBlindValue = new javax.swing.JTextField();
      jLabel3 = new javax.swing.JLabel();
      jTextFieldNumberOfPlayers = new javax.swing.JTextField();
      jLabel4 = new javax.swing.JLabel();
      jTextFieldNumberOfObs = new javax.swing.JTextField();
      jButton1 = new javax.swing.JButton();
      jLabel5 = new javax.swing.JLabel();
      jTextFieldTableName = new javax.swing.JTextField();
      textField = new javax.swing.JTextField();
      textField.setColumns(10);
      setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
      setTitle("Nowy stolik");

      jLabelCreateTabel.setFont(new java.awt.Font("Tahoma", 1, 18));
      jLabelCreateTabel.setText("Utwórz nowy stół");
      
      //obsługa przycisków "ok", "anuluj" i "wyczyść"
      
      jButtonOK.setText("OK");
      jButtonOK.addActionListener(new java.awt.event.ActionListener() {
          public void actionPerformed(java.awt.event.ActionEvent evt) {
              jButtonOKActionPerformed(evt);
          }
      });

      jButtonCancel.setText("Anuluj");
      jButtonCancel.addActionListener(new java.awt.event.ActionListener() {
          public void actionPerformed(java.awt.event.ActionEvent evt) {
              jButtonCancelActionPerformed(evt);
          }
      });
      
      jButton1.setText("Wyczyść");
      jButton1.addActionListener(new java.awt.event.ActionListener() {
          public void actionPerformed(java.awt.event.ActionEvent evt) {
              jButton1ActionPerformed(evt);
          }
      });

      //TODO: Dodać potem pobieranie aktualnej kwoty
      //wyświetlanie instrukcji dla gracza wraz z kryteriami wyboru
      
      jLabelRequirement.setText("Podaj minimalna ilość żetonów, jaką musi mieć grać aby usiąsć ("+Game_Cfg.VALUE_BIG_BLIND[0]+"- KWOTA_GRACZA");

      jLabel1.setText("Podaj wartość małej ciemnej ("+Game_Cfg.VALUE_SMALL_BLIND[0]+"-"+Game_Cfg.VALUE_SMALL_BLIND[1]+")");

      jLabel2.setText("Podaj wartość dużej ciemnej ("+Game_Cfg.VALUE_BIG_BLIND[0]+"-"+Game_Cfg.VALUE_BIG_BLIND[1]+")");

      jLabel3.setText("Podaj ilość graczy ("+Game_Cfg.TAB_PLAYER_LIM[0]+"-"+Game_Cfg.TAB_PLAYER_LIM[1]+")");

      jLabel4.setText("Podaj ilość obserwatorów ("+Game_Cfg.TAB_OBSERV_LIM[0]+"-"+Game_Cfg.TAB_OBSERV_LIM[1]+")");

      //proponujemy domyślną nazwę stołu - wolną nazwę z dużym p-stwem daje nam wylosowanie liczb i dopisanie ich iloczynu
      
      jLabel5.setText("Podaj nazwę stołu ("+Game_Cfg.TAB_NAME_LIM[0]+"-"+Game_Cfg.TAB_NAME_LIM[1]+" znaków)");

      int first = (int)(Math.random() * 100);
      int second = (int)(Math.random()*100);
      
      int tableAddition = first * second * first * second;
   
      //ustawienie wartości domyślnych pozostałych pól
      
      textField.setText(""+Game_Cfg.PLAYER_MOVE_TIME[2]);
      jTextFieldBigBlindValue.setText(""+Game_Cfg.VALUE_BIG_BLIND[2]);
      jTextFieldNumberOfObs.setText(""+Game_Cfg.TAB_OBSERV_LIM[2]);
      jTextFieldNumberOfPlayers.setText(""+Game_Cfg.TAB_PLAYER_LIM[0]);
      jTextFieldSmallBlindValue.setText(""+Game_Cfg.VALUE_SMALL_BLIND[2]);
      jTextFieldTableName.setText("Stół "+tableAddition);
      //TODO: Dodać potem pobieranie aktualnej kwoty
      minimumCash.setText("KWOTA_AKTUALNA_GRACZA");
      
      JLabel lblPodajCzasNa = new JLabel("Podaj czas na ruch gracza ("+Game_Cfg.PLAYER_MOVE_TIME[0]+"-"+Game_Cfg.PLAYER_MOVE_TIME[1]+" s)");
      
      
      
      
      //ustalenie parametrów graficznych okna
      
      javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
      layout.setHorizontalGroup(
      	layout.createParallelGroup(Alignment.LEADING)
      		.addGroup(layout.createSequentialGroup()
      			.addContainerGap(23, Short.MAX_VALUE)
      			.addGroup(layout.createParallelGroup(Alignment.LEADING)
      				.addComponent(jLabelCreateTabel, GroupLayout.PREFERRED_SIZE, 226, GroupLayout.PREFERRED_SIZE)
      				.addGroup(layout.createSequentialGroup()
      					.addGap(10)
      					.addGroup(layout.createParallelGroup(Alignment.LEADING)
      						.addComponent(jLabel5, GroupLayout.PREFERRED_SIZE, 204, GroupLayout.PREFERRED_SIZE)
      						.addComponent(jTextFieldTableName, GroupLayout.PREFERRED_SIZE, 293, GroupLayout.PREFERRED_SIZE)
      						.addComponent(jLabelRequirement)
      						.addComponent(minimumCash, GroupLayout.PREFERRED_SIZE, 295, GroupLayout.PREFERRED_SIZE)
      						.addGroup(layout.createSequentialGroup()
      							.addComponent(jButtonOK, GroupLayout.PREFERRED_SIZE, 59, GroupLayout.PREFERRED_SIZE)
      							.addGap(18)
      							.addComponent(jButtonCancel)
      							.addGap(18)
      							.addComponent(jButton1)))))
      			.addGap(74)
      			.addGroup(layout.createParallelGroup(Alignment.LEADING)
      				.addComponent(jTextFieldBigBlindValue, GroupLayout.PREFERRED_SIZE, 237, GroupLayout.PREFERRED_SIZE)
      				.addComponent(jLabel2, GroupLayout.PREFERRED_SIZE, 273, GroupLayout.PREFERRED_SIZE)
      				.addComponent(jTextFieldSmallBlindValue, GroupLayout.PREFERRED_SIZE, 237, GroupLayout.PREFERRED_SIZE)
      				.addComponent(jLabel1, GroupLayout.PREFERRED_SIZE, 295, GroupLayout.PREFERRED_SIZE)
      				.addGroup(layout.createSequentialGroup()
      					.addGroup(layout.createParallelGroup(Alignment.LEADING)
      						.addComponent(jLabel3)
      						.addComponent(lblPodajCzasNa)
      						.addComponent(jLabel4))
      					.addGap(12)
      					.addGroup(layout.createParallelGroup(Alignment.LEADING, false)
      						.addComponent(textField, 0, 0, Short.MAX_VALUE)
      						.addComponent(jTextFieldNumberOfObs, GroupLayout.DEFAULT_SIZE, 69, Short.MAX_VALUE)
      						.addComponent(jTextFieldNumberOfPlayers)))))
      );
      layout.setVerticalGroup(
      	layout.createParallelGroup(Alignment.LEADING)
      		.addGroup(layout.createSequentialGroup()
      			.addGroup(layout.createParallelGroup(Alignment.TRAILING)
      				.addGroup(layout.createSequentialGroup()
      					.addComponent(jLabelCreateTabel, GroupLayout.PREFERRED_SIZE, 38, GroupLayout.PREFERRED_SIZE)
      					.addGap(11))
      				.addGroup(layout.createSequentialGroup()
      					.addGroup(layout.createParallelGroup(Alignment.BASELINE)
      						.addComponent(lblPodajCzasNa)
      						.addComponent(textField, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
      					.addPreferredGap(ComponentPlacement.RELATED)))
      			.addGroup(layout.createParallelGroup(Alignment.BASELINE)
      				.addComponent(jLabel3)
      				.addComponent(jTextFieldNumberOfPlayers, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
      				.addComponent(jLabel5))
      			.addPreferredGap(ComponentPlacement.RELATED)
      			.addGroup(layout.createParallelGroup(Alignment.BASELINE)
      				.addComponent(jLabel4, GroupLayout.PREFERRED_SIZE, 26, GroupLayout.PREFERRED_SIZE)
      				.addComponent(jTextFieldNumberOfObs, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
      				.addComponent(jTextFieldTableName, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
      			.addPreferredGap(ComponentPlacement.RELATED)
      			.addGroup(layout.createParallelGroup(Alignment.LEADING)
      				.addComponent(jLabelRequirement)
      				.addComponent(jLabel1, GroupLayout.PREFERRED_SIZE, 23, GroupLayout.PREFERRED_SIZE))
      			.addPreferredGap(ComponentPlacement.RELATED)
      			.addGroup(layout.createParallelGroup(Alignment.LEADING)
      				.addComponent(minimumCash, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
      				.addComponent(jTextFieldSmallBlindValue, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
      			.addPreferredGap(ComponentPlacement.RELATED)
      			.addComponent(jLabel2, GroupLayout.PREFERRED_SIZE, 24, GroupLayout.PREFERRED_SIZE)
      			.addPreferredGap(ComponentPlacement.RELATED)
      			.addComponent(jTextFieldBigBlindValue, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
      			.addPreferredGap(ComponentPlacement.RELATED, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
      			.addGroup(layout.createParallelGroup(Alignment.BASELINE)
      				.addComponent(jButtonOK)
      				.addComponent(jButtonCancel)
      				.addComponent(jButton1))
      			.addContainerGap(GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
      );
      getContentPane().setLayout(layout);

      pack();
  }
  
  //wczytuje wejściowe parametry stołu takie jak ilość graczy, mała, duża ciemna, nazwa stołu etc.
  
  public int giveCashRequirement() {

      try {
          cash = Integer.parseInt(minimumCash.getText());
      } catch (NumberFormatException e) {
      }
      return cash;

  }

  public int giveBigBlindRequirement() {

      try {
          bigBlind = Integer.parseInt(jTextFieldBigBlindValue.getText());
      } catch (NumberFormatException e) {
      }
      return bigBlind;

  }

  public int numberPlayersRequirement() {

      try {
          numberPlayers = Integer.parseInt(jTextFieldNumberOfPlayers.getText());
      } catch (NumberFormatException e) {
      }
      return numberPlayers;
  }
      
  public int numberObsRequirement() {

      try {
          numberObs = Integer.parseInt(jTextFieldNumberOfObs.getText());
      } catch (NumberFormatException e) {
      }
      return numberObs;
  }
  
  public String tableNameRequirement() {

      tableName = jTextFieldTableName.getText();
      
      return tableName;
  }
  public int giveSmallBlindRequirement() {

      try {
          smallBlind = Integer.parseInt(jTextFieldSmallBlindValue.getText());
      } catch (NumberFormatException e) {
      }
      return smallBlind;

  }
  
  public int giveTimeToMove() {

      try {
          timeToMove = Integer.parseInt(textField.getText());
      } catch (NumberFormatException e) {
      }
      return timeToMove;

  }
  //sprawdzenie poprawności wpisanych parametrów
  
  private void jButtonOKActionPerformed(java.awt.event.ActionEvent evt) {  
  	giveCashRequirement();
      giveSmallBlindRequirement();
      giveBigBlindRequirement();
      numberPlayersRequirement();
      numberObsRequirement();
      tableNameRequirement();
      giveTimeToMove();
      
      //TODO:Dodać potem pobieranie aktualnej kwoty
      //W miejsce drugie warunkou będzie aktualna kwota gracza.
      if (cash < Game_Cfg.VALUE_BIG_BLIND[0] /* || cash > Game_Cfg.PLAYER_FUNDS_LOG */) {
          JOptionPane.showMessageDialog(null, "Niepoprawna ilość żetonów");
      } else if (smallBlind < Game_Cfg.VALUE_SMALL_BLIND[0] || smallBlind > Game_Cfg.VALUE_SMALL_BLIND[1] || smallBlind >= cash) {
          JOptionPane.showMessageDialog(null, "Niepoprawna wartość Małej ciemnej");
      } else if (bigBlind < Game_Cfg.VALUE_BIG_BLIND[0] || bigBlind > Game_Cfg.VALUE_BIG_BLIND[1] || bigBlind >= cash) {
          JOptionPane.showMessageDialog(null, "Niepoprawna wartość Dużej ciemnej");
      } else if (numberObs < Game_Cfg.TAB_OBSERV_LIM[0] || numberObs > Game_Cfg.TAB_OBSERV_LIM[1]) {
          JOptionPane.showMessageDialog(null, "Niepoprawna ilość obserwatorów");
      } else if (numberPlayers < Game_Cfg.TAB_PLAYER_LIM[0] || numberPlayers > Game_Cfg.TAB_PLAYER_LIM[1]) {
          JOptionPane.showMessageDialog(null, "Niepoprawna ilość graczy");
      } else if (tableName.length() < Game_Cfg.TAB_NAME_LIM[0] || tableName.length() > Game_Cfg.TAB_NAME_LIM[1]) {
          JOptionPane.showMessageDialog(null, "Niepoprawna nazwa stołu");
      } else if (timeToMove < Game_Cfg.PLAYER_MOVE_TIME[0] || timeToMove > Game_Cfg.PLAYER_MOVE_TIME[1]) {
          JOptionPane.showMessageDialog(null, "Niepoprawny czas na ruch");
      } else {
          final TableGame tableGame = new TableGame();
          tableGame.setVisible(true);
          tableGame.setLocationRelativeTo(null);
          this.dispose();
          successTable = true;
          
      }
  }
     //obsługa przycisku "wyczyść" - czyscimy wszystkie pola tekstowe    
  private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
	minimumCash.setText("");
	jTextFieldBigBlindValue.setText("");
	jTextFieldNumberOfPlayers.setText("");
	jTextFieldNumberOfObs.setText("");
	jTextFieldTableName.setText("");
	jTextFieldSmallBlindValue.setText("");
	textField.setText("");
	
  }
      
  //obsługa pryzycisku "anuluj" - wracamy do listy stołów
  
  private void jButtonCancelActionPerformed(java.awt.event.ActionEvent evt) {                                              
      this.dispose();
      
      final TableListScreen guiTable = new TableListScreen();
      guiTable.setVisible(true);
      guiTable.setLocationRelativeTo(null);
      
  }                                             

  public static void main(String args[]) {
  	//zmieniamy styl okienek, jeżeli ten będzie niedostępny ładowany jest domyślny
      try {
          for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
              if ("Nimbus".equals(info.getName())) {
                  javax.swing.UIManager.setLookAndFeel(info.getClassName());
                  break;
              }
          }
      } catch (ClassNotFoundException ex) {
          java.util.logging.Logger.getLogger(CreateNewTable.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
      } catch (InstantiationException ex) {
          java.util.logging.Logger.getLogger(CreateNewTable.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
      } catch (IllegalAccessException ex) {
          java.util.logging.Logger.getLogger(CreateNewTable.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
      } catch (javax.swing.UnsupportedLookAndFeelException ex) {
          java.util.logging.Logger.getLogger(CreateNewTable.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
      }
    

      
      java.awt.EventQueue.invokeLater(new Runnable() {
          @Override
          public void run() {
        	  final CreateNewTable frame = new CreateNewTable();                
              frame.setVisible(true);
              frame.setLocationRelativeTo(null);                
              frame.addWindowListener(new java.awt.event.WindowAdapter() {
  
  
});
      
 
      
      frame.addWindowListener(
              new java.awt.event.WindowAdapter() {
                  @Override
                  public void windowClosing(java.awt.event.WindowEvent e) {
                	  final TableListScreen guiTable = new TableListScreen();
                      guiTable.setVisible(true);
                      guiTable.setLocationRelativeTo(null);
                    
                      
                  }
              }
      );
      
      
  }
  
});
  }

                 
  private javax.swing.JButton jButton1;
  private javax.swing.JButton jButtonCancel;
  private javax.swing.JButton jButtonOK;
  private javax.swing.JLabel jLabel1;
  private javax.swing.JLabel jLabel2;
  private javax.swing.JLabel jLabel3;
  private javax.swing.JLabel jLabel4;
  private javax.swing.JLabel jLabel5;
  private javax.swing.JLabel jLabelCreateTabel;
  private javax.swing.JLabel jLabelRequirement;
  private javax.swing.JTextField jTextFieldBigBlindValue;
  private javax.swing.JTextField jTextFieldNumberOfObs;
  private javax.swing.JTextField jTextFieldNumberOfPlayers;
  private javax.swing.JTextField jTextFieldSmallBlindValue;
  private javax.swing.JTextField jTextFieldTableName;
  private javax.swing.JTextField minimumCash;
  private javax.swing.JTextField textField;
}
